package com.lgst.carousel;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import java.util.List;

/**
 * Created by L on 2017/2/15.
 */

public class CarouselView extends RelativeLayout {
    private ViewPager mVp;
    private CarouselController mCarouselController;

    public CarouselView(Context context) {
        super(context);
    }

    public CarouselView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CarouselView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void init(List<CarouselInfo> ads){
        //初始化ViewPager
        mVp = new ViewPager(getContext());
        RelativeLayout.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(mVp,params);

        //初始化指示器
        CarouselIndicator indicator = new CarouselIndicator(getContext());
        RelativeLayout.LayoutParams params2 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params2.addRule(ALIGN_PARENT_BOTTOM,1);
        indicator.init(ads.size());
        addView(indicator,params2);

//        初始化控制器并启动轮播
        mCarouselController = new CarouselController(getContext());
        mCarouselController.init(mVp,indicator, ads).startCarousel();

    }


    public void setOnItemClickListener(CarouselController.OnItemClickListener onItemClickListener) {
        mCarouselController.setOnItemClickListener(onItemClickListener);
    }

    public void stopCarousel(){
        mCarouselController.stopCarousel();
    }

    public void startCarousel(){
        mCarouselController.startCarousel();
    }

    public boolean isRunning()
    {
        return mCarouselController.isRunning();
    }
}
